<template>
  <el-form>
    <el-form-item label="旧密码">
      <el-input v-model.trim="oldPassword" type="password" />
    </el-form-item>
    <el-form-item label="新密码">
      <el-input v-model.trim="newPassword"  type="password"  />
    </el-form-item>
    <el-form-item label="再一次输入新密码">
      <el-input v-model.trim="againNewPassword" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">密码修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {changePassword} from "@/api/pwd";
import {MessageBox} from "element-ui";
import store from "@/store";
import router from "@/router";

export default {
  name: 'PasswordChange',
  data() {
    return {
      oldPassword: '',
      newPassword: '',
      againNewPassword: ''
    }
  },
  methods: {
    submit() {
      if (this.newPassword!==this.againNewPassword){
        this.$message({
          message: '两次输入密码不一致',
          type: 'warning',
          duration: 5 * 1000
        })
      }else {
        //请求修改密码
        let para = {
          oldPassword: this.oldPassword,
          newPassword: this.newPassword
        }
        changePassword(para).then(
          res => {
            console.log(res)
            if (res.code===0){
              this.$message({
                message: '密码修改成功，请重新登录',
                type: 'success',
                duration: 5 * 1000
              })


              store.dispatch('user/logout').then(() => {
                router.push(`/login?redirect=${this.$route.fullPath}`)
              })

            }else {
              this.$message({
                message: res.msg,
                type: 'warning',
                duration: 5 * 1000
              })
            }
          }
        )
      }

    }
  }
}
</script>
